
package com.idealighter.game.roomcard.common;

import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.roomcard.struct.RoomCardRoomCfg;
import com.idealighter.game.roomcard.struct.RoomCardRoomCfgValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import lombok.Getter;

/**
 * 牌桌.
 * 
 * @date 2015年8月6日 下午8:49:25
 *
 */
public abstract class AbstractRoomCardTable<S extends AbstractRoomCardSeat<RCFGT, RCFGVT>, //
    RCFGT extends RoomCardRoomCfg<RCFGVT>, //
    RCFGVT extends RoomCardRoomCfgValue> extends AbstractTable {
  // 所属的房间
  private final AbstractRoomCardRoomWithTable<? extends AbstractRoomCardTable<S, RCFGT, RCFGVT>, //
      S, RCFGT, RCFGVT> room;
  // 桌子编号种子
  private static final AtomicLong tableIdSeed = new AtomicLong(System.currentTimeMillis());
  // 坐下的座位
  private final List<S> seats;
  private final List<S> byStanderSeats; // 旁观者座位
  @Getter
  private final int maxSeatNum; // 座位数量
  @Getter
  private final int maxByStanderNum; // 旁观者数量
  // 本局战绩
  public Map<Long, Long> wins = new HashMap<Long, Long>();

  /**
   * 桌子id构造函数.
   */
  public AbstractRoomCardTable(
      AbstractRoomCardRoomWithTable<? extends AbstractRoomCardTable<S, RCFGT, RCFGVT>, //
          S, RCFGT, RCFGVT> room) {
    super(tableIdSeed.incrementAndGet());
    this.room = room;
    this.maxSeatNum = room.getMaxSeatNum(); // 包含的座位数
    this.maxByStanderNum = room.getMaxByStanderNum(); // 旁观者数量

    List<S> seats = new ArrayList<S>();
    if (this.maxSeatNum > 0) {
      for (int i = 0; i < this.maxSeatNum; i++) {
        S seat = createSeat(this, i);
        seat.setByStander(false);
        seats.add(seat);
      }
    }
    List<S> byStanderSeats = new ArrayList<S>();
    if (this.maxByStanderNum > 0) {
      for (int i = 0; i < this.maxByStanderNum; i++) {
        S seat = createSeat(this, this.maxSeatNum + i);
        seat.setByStander(true);
        byStanderSeats.add(seat);
      }
    }
    // 初始化坐位
    this.seats = Collections.unmodifiableList(seats);
    // 初始化旁观者坐位
    this.byStanderSeats = Collections.unmodifiableList(byStanderSeats);
  }
  // protected abstract List<S> createSeat(AbstractRoomCardTable<S> table);

  protected abstract S createSeat(AbstractRoomCardTable<S, RCFGT, RCFGVT> table, int order);

  @SuppressWarnings("unchecked")
  @Override
  public AbstractRoomCardRoomWithTable<? extends AbstractRoomCardTable<S, RCFGT, RCFGVT>, //
      S, RCFGT, RCFGVT> room() {
    return room;
  }

  @Override
  public List<S> seats() {
    List<S> result = new ArrayList<S>();
    for (S item : seats) {
      result.add(item);
    }
    for (S item : byStanderSeats) {
      result.add(item);
    }
    return result;
  }

  /**
   * @Title getGameSeats.
   * @Description 获取游戏位置(非旁观)
   * @date 2018年6月1日 下午7:45:59
   * @param onlyTook 只查找被占用的位置
   * @return .
   */
  public List<S> getGameSeats(boolean onlyTook) {
    List<S> result = new ArrayList<S>();
    for (S item : seats) {
      if (onlyTook) {
        if (item.getPlayerId() > 0) {
          result.add(item);
        }
      } else {
        result.add(item);
      }
    }
    return result;
  }

  /**
   * @Title getSeat.
   * @Description 根据座位号获取座位
   * @date 2018年6月14日 下午4:12:53
   * @param order .
   * @return .
   */
  public S getSeat(int order) {
    S result = null;
    for (S seat : seats) {
      if (seat.order == order) {
        result = seat;
        break;
      }
    }
    if (result == null) {
      for (S seat : byStanderSeats) {
        if (seat.order == order) {
          result = seat;
          break;
        }
      }
    }
    return result;
  }
}
